﻿// ****************************************************************************
// <copyright file="MainPage.xaml.cs" company="GalaSoft Laurent Bugnion">
// Copyright © GalaSoft Laurent Bugnion 2010
// </copyright>
// ****************************************************************************
// <author>Laurent Bugnion</author>
// <email>laurent@galasoft.ch</email>
// <date>20.6.2010</date>
// <project>MultiTouch.Behaviors.WP7.Sample</project>
// <web>http://multitouch.codeplex.com/</web>
// <license>
// See http://multitouch.codeplex.com/license.
// </license>
// ****************************************************************************

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using Microsoft.Phone.Controls;

namespace MultiTouch.Behaviors.WP7.Sample
{
    public enum SettingsState
    {
        SettingsNormal,
        SettingsShown,
    }

    public partial class MainPage : PhoneApplicationPage
    {
        private SettingsState _settingsState;

        /// <summary>
        /// The <see cref="TouchBehavior" /> dependency property's name.
        /// </summary>
        public const string TouchBehaviorPropertyName = "TouchBehavior";

        /// <summary>
        /// Gets or sets the value of the <see cref="TouchBehavior" />
        /// property. This is a dependency property.
        /// </summary>
        public MultiTouchBehavior TouchBehavior
        {
            get
            {
                return (MultiTouchBehavior)GetValue(TouchBehaviorProperty);
            }
            set
            {
                SetValue(TouchBehaviorProperty, value);
            }
        }

        /// <summary>
        /// Identifies the <see cref="TouchBehavior" /> dependency property.
        /// </summary>
        public static readonly DependencyProperty TouchBehaviorProperty = DependencyProperty.Register(
            TouchBehaviorPropertyName,
            typeof(MultiTouchBehavior),
            typeof(MainPage),
            new PropertyMetadata(null));

        public MainPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;

            var behaviors = Interaction.GetBehaviors(MyImage);
            if (behaviors != null
                && behaviors.Count > 0)
            {
                TouchBehavior = behaviors[0] as MultiTouchBehavior;
            }
        }

        private void SettingsClick(object sender, MouseButtonEventArgs e)
        {
            ToggleState();
        }

        private void ResetClick(object sender, RoutedEventArgs e)
        {
            TouchBehavior.Reset();
            ToggleState();
        }

        private void ToggleState()
        {
            switch (_settingsState)
            {
                case Sample.SettingsState.SettingsNormal:
                    VisualStateManager.GoToState(this, "SettingsShown", true);
                    _settingsState = Sample.SettingsState.SettingsShown;
                    break;
                case Sample.SettingsState.SettingsShown:
                    var binding1 = MinimumScaleTextBox.GetBindingExpression(TextBox.TextProperty);
                    binding1.UpdateSource();
                    var binding2 = MaximumScaleTextBox.GetBindingExpression(TextBox.TextProperty);
                    binding2.UpdateSource();
                    VisualStateManager.GoToState(this, "SettingsNormal", true);
                    _settingsState = Sample.SettingsState.SettingsNormal;
                    break;
            }
        }

        private void OkClick(object sender, RoutedEventArgs e)
        {
            ToggleState();
        }
    }
}
